#include <iostream>
using std::cout;
using std::endl;

class Complex;//前向声明

class Point
{
public:
    /* explicit */
    Point(int x = 0,int y = 0)
    : _ix(x)
    , _iy(y)
    {
        cout << "Point构造函数" << endl;
    }

    Point(const Complex & rhs);

    void print() const{
        cout << "(" << _ix 
            << "," << _iy
            << ")" << endl;
    }

    Point(const Point & rhs)
    : _ix(rhs._ix)
    , _iy(rhs._iy)
    {
        cout << "Point拷贝构造" << endl;
    }
    
    Point & operator=(const Point & rhs){
        cout << "Point赋值运算符函数" << endl;
        _ix = rhs._ix;
        _iy = rhs._iy;
        return *this;
    }

    Point & operator=(int x){
        cout << "Point的特殊赋值运算符函数int" << endl;
        _ix = x;
        return *this;
    }

    Point & operator=(const Complex & rhs);


    operator int(){
        return _ix + _iy;
    }

private:
    int _ix;
    int _iy;
};


class Complex
{
public:
    Complex(int real,int image)
    : _real(real)
    , _image(image)
    {
        cout << "Complex构造函数" << endl;
    }

    void print() const{
        cout << _real << " + " << _image << "i" << endl;
    }

    operator Point(){
        cout << "operator Point()函数被调用" << endl;
        return Point(_real,_image);
    }

    friend class Point;
private:
    int _real;
    int _image;
};

Point::Point(const Complex & rhs)
: _ix(rhs._real)
, _iy(rhs._image)
{
    cout << "Point的特殊构造函数" << endl;
}


Point & Point::operator=(const Complex & rhs){
    cout << "Point的特殊赋值运算符函数Complex" << endl;
    _ix = rhs._real;
    _iy = rhs._image;
    return *this;
}


#if 0
void test0(){
    //隐式转换，相当于Point pt = Point(1);
    Point pt = 1;
    pt.print();

    //需要让Point对象转换成int型数据
    //用到了Point类的类型转换函数operator int
    int num = pt;
    /* int num = pt.operator int(); */
    cout << num << endl;
}
#endif

void test1(){
    Point pt(3,4);
    pt = 1;
    pt.print();
}

void test2(){
    Point pt(4,7);
    Complex cx(7,9);
    //1.类型转换函数让Complex类对象cx转换成一个Point对象
    //2.隐式转换思路，提供特定的构造函数
    //3.给Point准备一个特殊的赋值运算符函数
    //处理优先级3 > 1 > 2
    pt = cx;
    pt.print();
}

int main(void){
    test2();
    return 0;
}
